package net.jiastudy.biz.user.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;

import net.jiastudy.biz.user.entity.Role;
import net.jiastudy.biz.user.entity.User;

@Service
public interface RoleService {

	public Role createRole(Role role);

	@Caching(evict = { @CacheEvict(value = { "user-permission" }, allEntries = true),
			@CacheEvict(value = { "user-role" }, allEntries = true), })
	public Role updateRole(Role role);

	@Caching(evict = { @CacheEvict(value = { "user-permission" }, allEntries = true),
			@CacheEvict(value = { "user-role" }, allEntries = true), })
	public boolean addUserRoles(User user, List<Long> roleIds);

	@Caching(evict = { @CacheEvict(value = { "user-permission" }, allEntries = true),
			@CacheEvict(value = { "user-role" }, allEntries = true), })
	public boolean addUserRolesByCode(User user, List<String> roleCodes);

	public boolean checkCodeExists(String code, Long roleId);

	@Cacheable(value = "user-permission", key = "'userId:'+#userId", unless = "#result == null")
	public Set<String> findPermissionsByUserId(Long userId);

	@Cacheable(value = "user-role", key = "'userId:'+#userId", unless = "#result == null")
	public Set<String> findRoleCodesByUserId(Long userId);

	@Caching(evict = { @CacheEvict(value = { "user-permission" }, allEntries = true),
			@CacheEvict(value = { "user-role" }, allEntries = true), })
	public boolean deleteRole(Long id);

	public Role getRoleById(Long id);

	public Role getRoleByCode(String code);

	public Page<Role> searchRoles(Page<Role> page, Map<String, String> conditions, Sort sort);

	public Set<Role> findRolesByUserId(Long userId);

	public Set<Long> findRoleUserIdsByRoleId(Long roleId);

	public List<Role> findRolesAll(String keywords);

	public boolean isUserExistRole(Long userId, String roleCode);

	@Caching(evict = { @CacheEvict(value = { "user-permission" }, allEntries = true),
			@CacheEvict(value = { "user-role" }, allEntries = true), })
	public boolean addUserRoles(Long userId, Long roleId);
}
